1 using System;
2 using
System.Collections.Generic;
3 using
System.IO;
4 using
System.IO.Compression;
5 using
System.Linq;
6 using
System.Reflection;
7 using
System.Text;
8 using
System.Xml.Linq;
9
10 using
UnityEditor;
11 using
UnityEngine;
12
13 namespace
Tiled2Unity
14 {
15     
class ImportUtils
16     {
17         
public static string GetMaterialPath(string texName)
18         {
19             
return String.Format("Assets/Tiled2Unity/Materials/{0}.mat", Path.GetFileNameWithoutExtension(texName));
20         }
21
22         
public static string GetTexturePath(string filename)
23         {
24             
return Path.Combine("Assets/Tiled2Unity/Textures", filename);
25         }
26
27         
public static string GetMeshPath(string filename)
28         {
29             
return String.Format("Assets/Tiled2Unity/Meshes/{0}.obj", Path.GetFileNameWithoutExtension(filename));
30         }
31
32         
public static string GetXmlPath(string objName)
33         {
34             
return String.Format("Assets/Tiled2Unity/Imported/{0}.tiled2unity.xml", Path.GetFileNameWithoutExtension(objName));
35         }
36
37         
public static string GetPrefabPath(string objName)
38         {
39             
return String.Format("Assets/Tiled2Unity/Prefabs/{0}.prefab", Path.GetFileNameWithoutExtension(objName));
40         }
41
42         
public static string GetAttributeAsString(XElement elem, string attrName)
43         {
44             
return elem.Attribute(attrName).Value;
45         }
46
47         
public static string GetAttributeAsString(XElement elem, string attrName, string defaultValue)
48         {
49             XAttribute attr = elem.Attribute(attrName);
50             
if (attr == null)
51             {
52                 
return defaultValue;
53             }
54             
return GetAttributeAsString(elem, attrName);
55         }
56
57         
public static int GetAttributeAsInt(XElement elem, string attrName)
58         {
59             
return Convert.ToInt32(elem.Attribute(attrName).Value);
60         }
61
62         
public static int GetAttributeAsInt(XElement elem, string attrName, int defaultValue)
63         {
64             XAttribute attr = elem.Attribute(attrName);
65             
if (attr == null)
66             {
67                 
return defaultValue;
68             }
69             
return GetAttributeAsInt(elem, attrName);
70         }
71
72         
public static float GetAttributeAsFloat(XElement elem, string attrName)
73         {
74             
return Convert.ToSingle(elem.Attribute(attrName).Value);
75         }
76
77         
public static float GetAttributeAsFloat(XElement elem, string attrName, float defaultValue)
78         {
79             XAttribute attr = elem.Attribute(attrName);
80             
if (attr == null)
81             {
82                 
return defaultValue;
83             }
84             
return GetAttributeAsFloat(elem, attrName);
85         }
86
87         
public static string GetAttributeAsFullPath(XElement elem, string attrName)
88         {
89             
return Path.GetFullPath(elem.Attribute(attrName).Value);
90         }
91
92         
public static void ReadyToWrite(string path)
93         {
94             
// Creates directories in path if they don't exist
95             FileInfo info =
new FileInfo(path);
96             info.Directory.Create();
97
98             
// Make sure file is not readonly
99             
if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
100             {
101                 
throw new UnityException(String.Format("{0} is read-only", path));
102             }
103         }
104
105         
public static byte[] Base64ToBytes(string base64)
106         {
107             
return Convert.FromBase64String(base64);
108         }
109
110         
public static string Base64ToString(string base64)
111         {
112             
byte[] bytes = Convert.FromBase64String(base64);
113             
return Encoding.ASCII.GetString(bytes);
114         }
115
116         
// Bah! This won't work (at least yet) due to Mono being a bit behind the .Net libraries
117         
//public static byte[] GzipBase64ToBytes(string gzipBase64)
118         
//{
119         
// byte[] bytesFromBase64 = Convert.FromBase64String(gzipBase64);
120         
// MemoryStream streamCompressed = new MemoryStream(bytesFromBase64);
121
122         
// // Now, decompress the bytes
123         
// using (MemoryStream streamDecompressed = new MemoryStream())
124         
// using (GZipStream deflateStream = new GZipStream(streamCompressed, CompressionMode.Decompress))
125         
// {
126         
// deflateStream.CopyTo(streamDecompressed);
127         
// byte[] bytesDecompressed = streamDecompressed.ToArray();
128         
// return bytesDecompressed;
129         
// }
130         
//}
131
132     }
// end class
133
134     
public static class HelperExtensions
135     {
136         
// Mono does not support GZipStream.CopyTo method yet
137         
//public static long CopyTo(this Stream source, Stream destination)
138         
//{
139         
// byte[] buffer = new byte[2048];
140         
// int bytesRead;
141         
// long totalBytes = 0;
142         
// while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
143         
// {
144         
// destination.Write(buffer, 0, bytesRead);
145         
// totalBytes += bytesRead;
146         
// }
147         
// return totalBytes;
148         
//}
149     }
150 }


Creates directories in path if they don't exist

Make sure file is not readonly

Bah! This won't work (at least yet) due to Mono being a bit behind the .Net libraries

public static byte[] GzipBase64ToBytes(string gzipBase64)

{

byte[] bytesFromBase64 = Convert.FromBase64String(gzipBase64);

MemoryStream streamCompressed = new MemoryStream(bytesFromBase64);

Now, decompress the bytes

using (MemoryStream streamDecompressed = new MemoryStream())

using (GZipStream deflateStream = new GZipStream(streamCompressed, CompressionMode.Decompress))

{

deflateStream.CopyTo(streamDecompressed);

byte[] bytesDecompressed = streamDecompressed.ToArray();

return bytesDecompressed;

}

}

} end class

Mono does not support GZipStream.CopyTo method yet

public static long CopyTo(this Stream source, Stream destination)

{

byte[] buffer = new byte[2048];

int bytesRead;

long totalBytes = 0;

while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)

{

destination.Write(buffer, 0, bytesRead);

totalBytes += bytesRead;

}

return totalBytes;

}




Trò chơi đua xe động vật trong UNITY Engine 114.789 lượt xem

Gõ tìm kiếm nhanh...